<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;

class Hotel extends Controller{
    public function index(){//酒店显示页
        $date = Db::table("hotel")->where("type=0")->paginate(5);
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $page = $date->render();
        return view("Hotel/index",["date"=>$date,"username"=>$name]);
    }
    public function cont($hotel_id){//酒店子页面，
        $get_id = $hotel_id;
        $date = Db::table("hotel")->where("type=0")->field("name")->select();
        $time = date("Y-m-d",time());
        $room_release =Db::table("room_release")->where("hotel_id=$get_id")->select();//客房展示
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $c=array();
        for($i=0;$i<count($room_release);$i++){
            $c[$i]['r_img'] = explode("/",$room_release[$i]["r_img"]);
        }
        $special_catering=Db::table("special_catering")->where("hotel_id=$get_id")->select();//餐饮展示
        $cc=array();
        for($i=0;$i<count($special_catering);$i++){
            $cc[$i]['img'] = explode("/",$special_catering[$i]["img"]);
        }
        $conference_release=Db::table("conference_release")->where("hotel_id=$get_id")->select();//会议展示
        $ccc=array();
        for($i=0;$i<count($conference_release);$i++){
            $ccc[$i]['img'] = explode("/",$conference_release[$i]["img"]);
        }
        setcookie("hotel",$get_id,time()+86400,"/");
        return view("Hotel/cont",[
            "c"=>$c,
            "cc"=>$cc,
            "ccc"=>$ccc,
            "date"=>$date,
            "time"=>$time,
            "room_release"=>$room_release,
            "special_catering"=>$special_catering,
            "conference_release"=>$conference_release,
            "username"=>$name
        ]);
    }
    public function order(){
        $guest_room = Db::table("guest_room")->field("order_sn,id")->select();//客房订单
        $count=Db::table("guest_room")->count();
        for($aa=0;$aa<$count;$aa++){
            $guest_room[$aa]["room"] = Db::table("guest_room")->where("id=".$guest_room[$aa]["id"])->find();
        }
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $meeting = Db::table("meeting")->field("order_sn,id")->select();
        $count_1=Db::table("meeting")->count();
        for($aaa=0;$aaa<$count_1;$aaa++){
            $meeting[$aaa]["meet"] = Db::table("meeting")->where("id=".$meeting[$aaa]["id"])->find();
        }
        $restaurant = Db::table("restaurant")->field("order_sn,id")->select();
        $count_2=Db::table("restaurant")->count();
        for($aaaa=0;$aaaa<$count_2;$aaaa++){
            $restaurant[$aaaa]["rest"] = Db::table("restaurant")->where("id=".$restaurant[$aaaa]["id"])->find();
        }
//        dump($restaurant);
        return view("Hotel/order",[
            "guest_room"=>$guest_room,
            "meeting"=>$meeting,
            "restaurant"=>$restaurant,
            "username"=>$name
            ]
        );
    }
    public function room($hotel_id){//客房详情
        $get_id = $hotel_id;
        $hotel= Cookie::get("hotel");//酒店id
        $room_release =Db::table("room_release")->where("id=$get_id")->select();
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $cc = array();
        for($i=0;$i<count($room_release);$i++){
            $cc[$i]['r_img'] = explode("/",$room_release[$i]["r_img"]);
        }
        $_hotel =Db::table("hotel")->where("id=$hotel")->field("detailed")->select();
//        dump($_hotel);
        return view("Hotel/room",["cc"=>$cc,"room_release"=>$room_release,"_hotel"=>$_hotel,"username"=>$name]);
    }
    public function room_order($hotel_id){//客房订单页
        $get_id = $hotel_id;
//        $room_release =Db::table("hotel")->where("")->field("id")->select();
        $release =Db::table("room_release")->where("id=$get_id")->select();//查询客房单价
        $name = Cookie::get("username");//用户名
        $hotel= Cookie::get("hotel");//酒店id
        $order_sn=time().time();//订单号
        $room_name = $release[0]["r_name"];//客房名
        $date_time = $_POST["date_time"];//入住时间
        $book      = $_POST["book"];//预计退房时间
        $section   = $_POST["section"];//房间数
        $a = substr($date_time,3,2);
        $b = substr($book,3,2);
        $c = $release[0]["amounts"];
        $d = ($b-$a)*$section*$c;//总金额
        $data = array(
            "username"=>$name,
            "order_sn"=>$order_sn,
            "hotel_id"=>$hotel,
            "name"    =>$room_name,
            "quantity"=>$section,
            "date_time"=>$date_time,
            "book"     =>$book,
            "amount"   =>$d,

        );
        if($name==""){
            $this->success("您还没登录，不能进行以下操作", "Index/login","",1);
        }else{
            $test = Db::table("guest_room")->insert($data);
            if($test){
                $this->redirect('Hotel/index',['hotel_id'=>$hotel_id]);
            }else{
                echo 0;
            }
            return view("Hotel/index",["username"=>$name]);
        }
    }
    public function conference($hotel_id){//会议详情
        $get_id = $hotel_id;
        $conference_release = Db::table("conference_release")->where("id=$get_id")->select();
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $cc = array();
        for($i=0;$i<count($conference_release);$i++){
            $cc[$i]['img'] = explode("/",$conference_release[$i]["img"]);
        }
        return view("Hotel/conference",["cc"=>$cc,"conference_release"=>$conference_release,"username"=>$name]);
    }
    public function con_order($hotel_id){//会议订单
        $get_id = $hotel_id;
        $name = Cookie::get("username");//用户名
        $hotel= Cookie::get("hotel");//酒店id
        $hotel_release =Db::table("hotel")->where("id=$hotel")->field("detailed")->select();
//        dump($hotel_release);
        $conference_release = Db::table("conference_release")->where("id=$get_id")->select();
//        dump($conference_release);
        $money = $_POST["money"];//时间段价格
        $names  = $_POST["name"];//会议室名
        $date_time = $_POST["date_time"];//开始时间
        $book  = $_POST["book"];//结束时间
        $section = $_POST["section"];//会议人数
        $order_sn=time().time();//订单号
        $a = substr($date_time,3,2);
        $b = substr($book,3,2);
        $c = $money;
        $d = ($b-$a)*$section*$c;//总金额
        $address=$hotel_release[0]["detailed"];
        $data =array(
            "username"=>$name,
            "hotel_id"=>$hotel,
            "order_sn"=>$order_sn,
            "name"    =>$names,
            "quantity"=>$section,
            "address" =>$address,
            "amounts"=>$d,
            "star"    =>$date_time,
            "end"     =>$book
        );
//        dump($data);
        if($name==""){
            $this->success("您还没登录，不能进行以下操作", "Index/login","",1);
        }else{
            $test = Db::table("meeting")->insert($data);
            if($test){
                $this->redirect('Hotel/conference',['hotel_id'=>$hotel_id]);
            }else{
                echo 0;
            }
            return view("Hotel/order");
        }
//        return view("Hotel/order");
    }
    public function special($hotel_id){//餐饮详情
        $get_id = $hotel_id;
        $special_catering= Db::table("special_catering")->where("id=$get_id")->select();
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";

        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
        $cc = array();
        for($i=0;$i<count($special_catering);$i++){
            $cc[$i]['img'] = explode("/",$special_catering[$i]["img"]);
        }
//        dump($special_catering);
        return view("Hotel/special",["cc"=>$cc,"special_catering"=>$special_catering,"username"=>$name]);
    }
    public function spec_order($hotel_id){
        $get_id = $hotel_id;
        $name = Cookie::get("username");//用户名
        $hotel= Cookie::get("hotel");//酒店id
        $hotel_release =Db::table("hotel")->where("id=$hotel")->field("detailed")->select();
        $special_catering= Db::table("special_catering")->where("id=$get_id")->select();
        $names= $_POST["name"];//餐饮名
        $section=$_POST["section"];//用餐人数
        $date_time=$_POST["date_time"];//用餐时间
        $money=$special_catering[0]["amounts"];//餐饮价格
        $d =$money*$section;//总金额
        $order_sn = time().time();//订单号
        $address=$hotel_release[0]["detailed"];
        $date = array(
            "username"=>$name,
            "hotel_id"=>$hotel,
            "name"=>$names,
            "order_sn"=>$order_sn,
            "quantity"=>$section,
            "amounts"=>$d,
            "address"=>$address
        );
//        dump($date);
        if($name==""){
            $this->success("您还没登录，不能进行以下操作", "Index/login","",1);
        }else{
            $test = Db::table("restaurant")->insert($date);
            if($test){
                $this->redirect('Hotel/special',['hotel_id'=>$hotel_id]);
            }else{
                echo 0;
            }
            return view("Hotel/order");
        }
    }
}